package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.Constants;
import com.example.entry.OrderForm;
import com.example.entry.OrderGrabbing;
import com.example.entry.Rider;
import com.example.exception.ServiceException;
import com.example.mapper.OrderFormMapper;
import com.example.mapper.RiderMapper;
import com.example.service.IOrderGrabbingService;
import com.example.mapper.OrderGrabbingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 27223
* @description 针对表【order_grabbing(抢单表)】的数据库操作Service实现
* @createDate 2024-03-07 20:10:04
*/
@Service
public class OrderGrabbingServiceImpl extends ServiceImpl<OrderGrabbingMapper, OrderGrabbing>
    implements IOrderGrabbingService {

    @Autowired
    private OrderGrabbingMapper orderGrabbingMapper;

    @Autowired
    private OrderFormMapper orderFormMapper;

    @Autowired
    private RiderMapper riderMapper;

    /*
    * 骑手接单
    * */
    @Override
    public void riderConnect(OrderGrabbing orderGrabbing) {

        try {
            // 骑手抢单
            orderGrabbingMapper.updateById(orderGrabbing);

            // 订单显示骑手接单
            updateOrderForm(orderGrabbing);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_600,"数据插入错误！请重试!");
        }
    }

    /*
    * 用户订单显示骑手信息
    * */
    public void updateOrderForm(OrderGrabbing orderGrabbing){
        try {
            OrderForm orderForm = orderFormMapper.selectById(orderGrabbing.getOrderFormId());
            orderForm.setRiderId(orderGrabbing.getRiderId());
            orderForm.setRiderName(orderGrabbing.getRiderName());
            orderForm.setRiderPhone(orderGrabbing.getRiderPhone());
            // 根据订单id查询到具体的订单
            QueryWrapper<OrderForm> wrapper = new QueryWrapper<>();
            wrapper.eq("id", orderGrabbing.getOrderFormId());
            orderFormMapper.update(orderForm, wrapper);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_600, "订单数据插入错误");
        }
    }

    /*
    * 订单送达
    * */
    @Override
    public void orderComplete(OrderGrabbing orderGrabbing) {
        try {
            // 修改订单信息
            orderGrabbingMapper.updateById(orderGrabbing);

            // 修改用户订单表信息
            updateOrderFormStatus(orderGrabbing);

            // 修改骑手余额
            updateRiderMoney(orderGrabbing);
        }catch (Exception e){
            throw new ServiceException(Constants.CODE_600,"数据插入错误！，请重试");
        }
    }

    /*
    * 修改用户订单表信息
    * */
    public void updateOrderFormStatus(OrderGrabbing orderGrabbing){
        try {
            // 查询订单信息
            OrderForm orderForm = orderFormMapper.selectById(orderGrabbing.getOrderFormId());
            orderForm.setStatus(0);
            orderForm.setUpdateTime(orderGrabbing.getOverTime());
            QueryWrapper<OrderForm> wrapper = new QueryWrapper<>();
            wrapper.eq("id", orderGrabbing.getOrderFormId());
            orderFormMapper.update(orderForm, wrapper);
        }catch (Exception e){
            throw new ServiceException(Constants.CODE_600, "订单信息发生错误");
        }
    }

    /*
     * 修改骑手余额
     * */
    public void updateRiderMoney(OrderGrabbing orderGrabbing){
        try {
            Rider rider = riderMapper.selectById(orderGrabbing.getRiderId());
            rider.setMoney((float) (rider.getMoney() + orderGrabbing.getCommission()));
            QueryWrapper<Rider> wrapper = new QueryWrapper<>();
            wrapper.eq("id", orderGrabbing.getRiderId());
            riderMapper.update(rider, wrapper);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_600, "骑手余额发生错误");
        }
    }
}




